package com.zhiying.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.zhiying.domain.OS_Category;
import com.zhiying.page.CmsPageResult;
import com.zhiying.page.CmsResult;
import com.zhiying.page.PageInfo;
import com.zhiying.service.CategoryService;
import com.zhiying.until.code.CommonReturnCode;

@Controller
@RequestMapping("/product/category")
public class CategoryCaontroller {

	@Autowired
	private CategoryService categoryService;
	
	@RequiresPermissions("product:category:list")
	@ResponseBody
	@GetMapping("/")
	public Object showCategorylist(PageInfo pageInfo, @RequestParam(required = false, value = "search") String search) {
		int parentId=1;
		List<OS_Category> list=categoryService.SelectinfoTwo(pageInfo, search, parentId);	
		return new CmsPageResult(list,pageInfo.getTotal());
	
	}
	@RequiresPermissions("product:category:sonlist")
	@ResponseBody
	@GetMapping("/{parentid}")
	public Object SelectSonList(PageInfo pageInfo, @RequestParam(required = false, value = "search") String search,@PathVariable("parentid")int parentId) {
		List<OS_Category> list=categoryService.SelectinfoTwo(pageInfo, search, parentId);
		return new CmsPageResult(list,pageInfo.getTotal());
	}
	
	@RequiresPermissions("product:category:update")
	@ResponseBody
	@PutMapping("/{value}/audit")
	public Object UpdateType(@PathVariable("value")Long type) {
		OS_Category category=new OS_Category();
		category.setCategoryId(type);
		int row=categoryService.UpdateStatus(category);
		if(row>0) {
			return new CmsResult(CommonReturnCode.SUCCESS, row);
		}
		return new CmsResult(CommonReturnCode.BAD_REQUEST, row);
	}
	
	
	@RequiresPermissions("product:category:view")
	@RequestMapping("/view")
	public String showCategory() {
		
		return "category/product_category_list";
	}
	
	@DeleteMapping("/{value}")
	@ResponseBody
	public Object delectinfo(@PathVariable("value")int CategoryId) {
		int row=categoryService.DeleteinfoByid(CategoryId);
		if(row>0) {
			return new CmsPageResult(CommonReturnCode.SUCCESS, row);
		}
		return new CmsPageResult(CommonReturnCode.FAILED, row);
	}
}
